package cn.tuling.org.controller;

import cn.tuling.basic.util.AjaxResult;
import cn.tuling.basic.util.PageList;
import cn.tuling.org.domain.Novel;
import cn.tuling.org.query.NovelQuery;
import cn.tuling.org.service.NovelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/novel")
@Api(value = "用户的API",description="用户相关的CRUD功能")
public class NovelController {

    @Autowired
    private NovelService novelService;

    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Novel> findAll(){
        return novelService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询单条" )
    public Novel findById(@ApiParam(value="ID属性",required = true)  @PathVariable("id")Long id ){
        return novelService.findById(id);
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )
    public AjaxResult del(@PathVariable("id")Long id){
        novelService.deleteNovel(id);
        return AjaxResult.me().success();
    }

    @PutMapping
    @ApiOperation(value = "修改或添加" )
    public AjaxResult addOrUpdate(@RequestBody Novel novel){
        if (novel.getId()==null){
            novelService.addNovel(novel);
        }else {
            novelService.updateNovel(novel);
        }
        return AjaxResult.me().success();
    }


    @PostMapping
    public PageList<Novel> queryByPage(@RequestBody NovelQuery novelQuery){
        return novelService.queryByPage(novelQuery);
    }
}
